<?php
namespace Fwadmin\Controller;
use Think\Controller;
class LoginController extends Controller{
	public function index(){
		if($_SESSION['user_name']){
			$this -> assign('user_name', $_SESSION['user_name']);
			$this -> redirect(U('Index/index'));
		}
		$this -> display('login');
	}

	public function login(){
		$User = M('Manage');
		$user['username'] = trim($_GET['user_name']);
		$userPassword = trim($_GET['user_password']);
		if($user['username'] == '' || $userPassword == ''){
			$date['msg'] = '请输入用户名或密码！';
			$date['status'] = '0';
			$this->ajaxReturn($date);
		}else{
			$If_user = $User -> where(array('username' => $user['username'])) -> find();
			if($If_user){
				$password['username&password'] = array($user['username'],md5($userPassword),'_multi' => true);
				$If_password = $User -> where($password) -> select();
				if($If_password){
					cookie('name',$user["username"],array('expire'=>7200,'prefix'=>'yz_'));
					$data['username'] = $user['username'];
					$data['source'] = get_client_ip();
					$data['last_time'] = date("Y-m-d H:i:s");
					M('Adminlog') -> add($data);
					$date['callback'] = U('Index/index');
					$date['status'] = '1';
					$this->ajaxReturn($date);
					$this -> redirect(U('Index/index'));
				}else{
					$date['msg'] = '密码错误！';
					$date['status'] = '0';
					$this->ajaxReturn($date);
				}
			}else{
				$date['msg'] = '用户名不存在！';
				$date['status'] = '0';
				$this->ajaxReturn($date);
			}
		}
	}
}